package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthMoneyResourceDataService;
import com.orange.handler.AuthSchoolService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.modules.entity.school_data.AuthMoneyResource;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/moneyResource")
public class AuthMoneyResourceDataController extends BaseController{

	private static final Logger logger = Logger.getLogger(AuthMoneyResourceDataController.class);
	
	
	@Autowired
	private AuthMoneyResourceDataService authMoneyResourceDataService;
	
	@Autowired
	private AuthSchoolService authSchoolService;
	/**
	 * 新增经费资源
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertMoneyResource" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertMoneyResource(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==sid){
				return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
			}
			String subject = (String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);//学科

			String trainingFunds = map.get("trainingFunds");
			String remark1 = map.get("remark1");
			String professionalTrainingFunds = map.get("professionalTrainingFunds");
			String remark2 = map.get("remark2");
			String budgetStandards = map.get("budgetStandards");
			String remark3 = map.get("remark3");
			String wereBorn = map.get("wereBorn");
			String remark4 = map.get("remark4");
			String averageEducationFundingRatio  =map.get("averageEducationFundingRatio");
			String remark5 = map.get("remark5");
			String outlayPerStudent = map.get("outlayPerStudent");
			String remark6 = map.get("remark6");
			String equipmentInvestment = map.get("equipmentInvestment");
			String remark7 = map.get("remark7");
			String professionalLibraryFunding = map.get("professionalLibraryFunding");
			String remark8 = map.get("remark8");
			String fundingEducationFundingPer = map.get("fundingEducationFundingPer");
			String remark9 = map.get("remark9");
			//String remark = map.get("remark");
			String status ="0";
			if(trainingFunds==null || StringUtils.isBlank(trainingFunds))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"全校(院)上年生均培养经费不能为空");
			if(professionalTrainingFunds==null || StringUtils.isBlank(professionalTrainingFunds))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业上年生均培养经费不能为空");
			if(budgetStandards==null || StringUtils.isBlank(budgetStandards))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"最新教育实习经费预算标准不能为空");
			if(wereBorn==null || StringUtils.isBlank(wereBorn))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"上年教育实习经费支出生均数不能为空");
			if(averageEducationFundingRatio==null || StringUtils.isBlank(averageEducationFundingRatio))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"全校教师上年培训经费总额占生均教育经费拨款比例不能为空");
			if(outlayPerStudent==null || StringUtils.isBlank(outlayPerStudent))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业教师上年培训经费支出总额占生均教育经费拨款比例不能为空");
			if(equipmentInvestment==null || StringUtils.isBlank(equipmentInvestment))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业上年教学设施设备投入生均数不能为空");
			if(professionalLibraryFunding==null || StringUtils.isBlank(professionalLibraryFunding))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业上年专业图书经费投入生均数不能为空");
			if(fundingEducationFundingPer==null || StringUtils.isBlank(fundingEducationFundingPer))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"本专业学校拨款的教育科研经费生均数不能为空");
			if(sid==null || StringUtils.isBlank(sid))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校ID不能为空");
					
			AuthMoneyResource amr = new AuthMoneyResource();
			amr.setAverageEducationFundingRatio(averageEducationFundingRatio);
			amr.setBudgetStandards(budgetStandards);
			amr.setCreatetime(sdf.format(new Date()));
			amr.setEquipmentInvestment(equipmentInvestment);
			amr.setFundingEducationFundingPer(fundingEducationFundingPer);
			amr.setLogintype(subject);
			amr.setOutlayPerStudent(outlayPerStudent);
			amr.setProfessionalLibraryFunding(professionalLibraryFunding);
			amr.setProfessionalTrainingFunds(professionalTrainingFunds);
			amr.setSid(sid);
			amr.setStatus(status);
			amr.setTrainingFunds(trainingFunds);
			amr.setWereBorn(wereBorn);
			String remark = remark1 + "#" +remark2+"#" +remark3+ "#" +remark4+ "#" +remark5+ "#" +remark6+ "#" +remark7+ "#" +remark8+ "#" +remark9;
			amr.setRemark(remark);
			authMoneyResourceDataService.insertMoneyResource(amr);
			AuthSchool as = authSchoolService.queryAuthSchoolInfoBypid(sid);
			
			amr.setLogintype(subject);
			
			if("1".equals(status)){
				if("1101".equals(subject) ){ //如果是理科
					if(as.getScience().equals("2"))  //如果是登陆过 
						as.setScience("4");
					if(as.getScience().equals("3"))   //如果填写过基础数据状态表
						as.setScience("5");
				}
				if("1102".equals(subject) ){ //如果是文科
					if(as.getLiberalarts().equals("2"))  //如果是登陆过 
						as.setLiberalarts("4");
					if(as.getLiberalarts().equals("3"))   //如果填写过基础数据状态表
						as.setLiberalarts("5");
				}
				if("1103".equals(subject) ){ //如果是整体申报
					if(as.getWholeDeclaration().equals("2"))  //如果是登陆过 
						as.setWholeDeclaration("4");
					if(as.getWholeDeclaration().equals("3"))   //如果填写过基础数据状态表
						as.setWholeDeclaration("5");
				}
			}
			authSchoolService.updateAuthSchoolInfo(as);
			return toResponse(amr,ReturnCode.SUCCESS,"新增成功");
		} catch (Exception e) {
			logger.error(e);
			return toResponse(null,ReturnCode.FAIL,"新增失败");
		}
	}
	
	/**
	 * 根据当前用户查询经费数据
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryMoneyResourceDataBySchoolId" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryMoneyResourceDataBySchoolId(HttpServletRequest request) throws SQLException{
		String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(null==sid){
			return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
		}
		String subject = (String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);//学科
		
		AuthMoneyResource amr = authMoneyResourceDataService.queryAuthMoneyResourcesBySchoolId(sid, subject);
		return toResponse(amr,ReturnCode.SUCCESS,"查询成功");
	}
	
	
	
	
	
	
	@RequestMapping(value = "/updateMoneyResourceData" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateMoneyResourceData(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(null==sid){
			return toResponse(null,ReturnCode.NOLOGIN,"未登陆");
		}
		String subject = (String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);//学科
		
		try {
			AuthMoneyResource amr = authMoneyResourceDataService.queryAuthMoneyResourcesBySchoolId(sid, subject);
			if(amr!=null){
				String trainingFunds = map.get("trainingFunds");
				String remark1 = map.get("remark1");
				String professionalTrainingFunds = map.get("professionalTrainingFunds");
				String remark2 = map.get("remark2");
				String budgetStandards = map.get("budgetStandards");
				String remark3 = map.get("remark3");
				String wereBorn = map.get("wereBorn");
				String remark4 = map.get("remark4");
				String averageEducationFundingRatio  =map.get("averageEducationFundingRatio");
				String remark5 = map.get("remark5");
				String outlayPerStudent = map.get("outlayPerStudent");
				String remark6 = map.get("remark6");
				String equipmentInvestment = map.get("equipmentInvestment");
				String remark7 = map.get("remark7");
				String professionalLibraryFunding = map.get("professionalLibraryFunding");
				String remark8 = map.get("remark8");
				String fundingEducationFundingPer = map.get("fundingEducationFundingPer");
				String remark9 = map.get("remark9");
				
				if(trainingFunds!=null && StringUtils.isNotBlank(trainingFunds))
					amr.setTrainingFunds(trainingFunds);
				if(professionalTrainingFunds!=null && StringUtils.isNotBlank(professionalTrainingFunds))
					amr.setProfessionalTrainingFunds(professionalTrainingFunds);
				if(budgetStandards!=null && StringUtils.isNotBlank(budgetStandards))
					amr.setBudgetStandards(budgetStandards);
				if(wereBorn!=null && StringUtils.isNotBlank(wereBorn))
					amr.setWereBorn(wereBorn);
				if(averageEducationFundingRatio!=null && StringUtils.isNotBlank(averageEducationFundingRatio))
					amr.setAverageEducationFundingRatio(averageEducationFundingRatio);
				if(outlayPerStudent!=null && StringUtils.isNotBlank(outlayPerStudent))
					amr.setOutlayPerStudent(outlayPerStudent);
				if(equipmentInvestment!=null && StringUtils.isNotBlank(equipmentInvestment))
					amr.setEquipmentInvestment(equipmentInvestment);
				if(professionalLibraryFunding!=null && StringUtils.isNotBlank(professionalLibraryFunding))
					amr.setProfessionalLibraryFunding(professionalLibraryFunding);
				if(fundingEducationFundingPer!=null && StringUtils.isNotBlank(fundingEducationFundingPer))
					amr.setFundingEducationFundingPer(fundingEducationFundingPer);
				String remark = remark1 + "#" +remark2+ "#" +remark3+ "#" +remark4+ "#" +remark5+ "#" +remark6+ "#" +remark7+ "#" +remark8+ "#" +remark9;
				amr.setRemark(remark);	
				amr.setStatus("1");
				authMoneyResourceDataService.insertMoneyResource(amr);
			}
			return toResponse(amr,ReturnCode.SUCCESS,"修改成功");
		} catch (Exception e) {
			return toResponse(null,ReturnCode.FAIL,"修改失败");
		}
		
	}
	/**********************************************************************************************************/
	/**
	 * 根据当前用户查询经费数据
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryMoneyResourceDataBySchoolId1" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryMoneyResourceDataBySchoolId1(HttpServletRequest request,@RequestParam String schoolid) throws SQLException{
		try {
			AuthMoneyResource amr = authMoneyResourceDataService.queryAuthMoneyResourcesBySchoolId(schoolid, null);
			return toResponse(amr,ReturnCode.SUCCESS,"查询经费数据成功");
		} catch (Exception e) {
			logger.error("查询经费数据失败",e);
			return toResponse(null,ReturnCode.FAIL,"查询经费数据失败");
		}
	}
	
}
